package com.ysp.summary.excel;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("excel")
public class GenerateSql {

    /**
     * 生成修改导购的脚本
     */
    @PostMapping("generateSql")
    public void generateSql(@RequestParam(value = "file") MultipartFile file
    ) throws IOException {
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream(),0);   //excel的sheet索引是从 0 开始
        List<List<Object>> list = reader.read(1);   //读取数据的时候  也是从0行开始
        JSONArray jsonArray = JSONUtil.parseArray(list);
        JSONObject jsonObject = JSON.parseObject(jsonArray.toString());
//        ExcelReader reader2 = ExcelUtil.getReader(file.getInputStream(),1);
//        List<List<Object>> list2 = reader.read(1);
//        list.addAll(list2);
        if (CollectionUtil.isNotEmpty(list)){
            list.forEach(x ->{
                System.out.println("UPDATE login_user SET service_staff_id = '48' where telephone = '"+x.get(0)+"' AND deleted =1;");
            });
        }
    }

    public static void main(String[] args) {
        List<Map<String,String>> list =new  ArrayList(8);
        Map<String,String> map = new HashMap<>(8);
        for (int i = 0; i < 5; i++) {
            map.put("lao"+i++,"兰州");
            map.put("yang"+i++,"深圳");
            map.put("liu"+i++,"平凉");
            list.add(map);
        }
        net.sf.json.JSONArray jsonObject = net.sf.json.JSONArray.fromObject(list);
        net.sf.json.JSONObject jsonObject1 = net.sf.json.JSONObject.fromObject(jsonObject);
        com.alibaba.fastjson.JSONArray json =new  com.alibaba.fastjson.JSONArray(Collections.singletonList(list));
//        JSONObject jsonObject = JSONObject.parseObject((String) json);
        System.out.println(jsonObject1);
    }

}
